package com.tanhua.manage.controller;

import com.tanhua.manage.service.UsersService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/manage")
public class UsersController {

    @Autowired
    private UsersService usersService;

    /**
     * 分页查询app所有的用户
     *      get  /users
     * 参数：
     *    page
     *    pagesize
     */
    @GetMapping("/users")
    public ResponseEntity users(@RequestParam(defaultValue = "1") int page,
                                @RequestParam(defaultValue = "10") int pagesize) {
        return usersService.findAllUsers(page,pagesize);
    }

    /**
     * 获取指定APP用户的详情
     *  GET     /users/{id}
     */
    @GetMapping("/users/{id}")
    public ResponseEntity findById(@PathVariable("id") Long userId){
        return usersService.findById(userId);
    }

    /**
     * 获取当前用户的所有视频分页列表
     *  GET     /videos
     *  参数：   pagesize，size，uid
     */
    @GetMapping("/videos")
    public ResponseEntity findAllVideos(@RequestParam(defaultValue = "1") int page,
                                        @RequestParam(defaultValue = "10") int pagesize,
                                        @RequestParam(required = false) Long uid) {
        return usersService.findAllVideos(page,pagesize,uid);
    }

    /**
     * 查询动态分页列表
     *  get  /messages
     *  参数
     *     pagesize，size，uid  ，state
     */
    @GetMapping("/messages")
    public ResponseEntity findAllMovements(@RequestParam(defaultValue = "1") int page,
                                        @RequestParam(defaultValue = "10") int pagesize,
                                        @RequestParam(required = false) Long uid,
                                        @RequestParam(required = false) String state) {
        Integer publishState = null;
        if(!StringUtils.isEmpty(state)) {
            publishState = Integer.parseInt(state);
        }
        return usersService.findAllMovements(page,pagesize,uid,publishState);
    }

    /**
     * 根据id查询，动态详情
     *  get /messages/5e8309085a319e0d4203efd0
     */
    @GetMapping("/messages/{publishId}")
    public ResponseEntity findMovementById(@PathVariable("publishId") String publishId) {
        return usersService.findMovementById(publishId);
    }

    /**
     * 查看动态的评论列表
     *  GET     /messages/comments
     *  messageID : 动态id（publishId）
     */
    @GetMapping("/messages/comments")
    public ResponseEntity findAllComments(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pagesize,
            String messageID) {
        return usersService.findAllComments(page,pagesize,messageID);
    }
}
